<?php
/**
 * AccountController.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-16 03:44:12
 * @modified    2023-02-16 03:44:12
 *
 */

namespace App\Http\Controllers;

use App\Services\AccountService;
use App\Services\VerifyCodeServ;
use Illuminate\Http\Request;

class AccountController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->service = new AccountService();
    }

    /**
     * @OA\Get(
     *     path="/account/info",
     *     tags={"Account"},
     *     summary="获取前台登录用户信息",
     *     description="获取前台登录用户信息",
     *     operationId="account_info",
     *     security={{"bearerAuth":{}}},
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function info()
    {
        try {
            $data = $this->service->info();
            return message('获取成功', true, $data);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    public function login(Request $request)
    {
        $redirect = $request->query('redirect') ?? '';
        return view('pages.account.login', ['redirect' => $redirect]);
    }

    public function storageUser(Request $request)
    {
        $account = $request->get('account', '');
        $verifyCode = $request->get('verify_code', '');
        $verifyCodeServ = new VerifyCodeServ($account);
        $verifyCodeServ->validateCode($verifyCode);
        session()->put(VerifyCodeServ::CODE_KEY, $account);
        return response()->json(['message' => true]);
    }
}